<template>
  <el-dialog
    v-if="dialogShow"
    v-model="dialogShow"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    append-to-body
    custom-class="mydialog"
    width="1200px"
    title="变更产品价格--添加终端"
    @close="close"
  >
    <el-form
      ref="formRef"
      :model="addClientForm"
      :label-width="formLabelWidth"
      :inline="true"
      size="small"
      :rules="formRules"
    >
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item
            label="价格编号"
            style="width:100%"
          >
            <el-select
              v-model="addClientForm.productPriceId"
              style="width: 100%"
              filterable
              placeholder="请选择价格编号"
              @change="filterChange"
            >
              <el-option
                v-for="(item,index) in priceNoOptions"
                :key="item"
                :label="item.priceNumber"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="所在地区"
            style="width:100%"
          >
            <el-cascader
              v-model="addClientForm.region"
              :popper-append-to-body="false"
              style="width: 100%;"
              :options="citys"
              placeholder="请选择所在地区"
              :props="{ multiple: true, expandTrigger: 'hover', label: 'regionName', value:'regionCode' }"
              collapse-tags
              clearable
              @change="areaChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="终端类型"
            style="width:100%"
          >
            <el-select
              v-model="addClientForm.terminalType"
              style="width: 100%"
              placeholder="请选择终端类型"
              clearable
              @change="filterChange"
            >
              <el-option
                v-for="item in terminalData"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      ref="multipleTable"
      header-cell-class-name="table-header"
      :data="clientListData"
      :border="true"
      style="width: 100%"
      max-height="400px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="55"
      />
      <el-table-column
        label="终端编号"
        prop="terminalNo"
        header-align="center"
        align="center"
      >
        <template #default="scope">{{ scope.row.terminalNo }}</template>
      </el-table-column>
      <el-table-column
        label="终端名称"
        prop="terminalName"
        header-align="center"
      />
      <el-table-column
        label="所在地区"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          <div>{{ scope.row.provinceName }} / {{ scope.row.cityName }} / {{ scope.row.areaName }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="原市场价（元）"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          <div>{{ scope.row.productInfoDetail.marketPrice }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="原终端价（元）"
        prop="yzdj"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          <div>{{ scope.row.terminalPrice }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="准入模式"
        prop="accessMode"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          <div>
            {{ getLabel('access_mode', scope.row.accessMode) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="原关联产品价格"
        prop="priceNumber"
        header-align="center"
        align="center"
      />
      <el-table-column
        label="营销模式"
        prop="sellMode"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          <span v-if="scope.row?.sellMode !== 'direct'">
            {{ scope.row?.agentAttribute === '1' ?'独家招商':'精细招商' }}
          </span>
          <span v-else> 终端直销</span>
        </template>
      </el-table-column>
      <el-table-column
        label="渠道客户名称"
        prop="qdkhmc"
        header-align="center"
      >
        <template #default="scope">
          <div>
            {{ scope.row.agentDetail?.agentName }}
          </div>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <div
        class="dialog-footer"
        style="display:flex;justify-content:center;align-items:center;"
      >
        <el-button
          class="border-btn"
          size="small"
          @click="close"
        >取消</el-button>
        <el-button
          class="main-btn"
          size="small"
          type="primary"
          @click="confirm"
        >确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {defineComponent, inject, reactive, ref, toRefs} from 'vue'
import {crmStore} from '@CRM/store'
import {getScreenAreaData} from '@/utils/areaFuc.js'

export default defineComponent({
  name: 'CreateOrChange',
  emits: ['selectSuccess'],
  setup(props, context) {
    const formRef = ref(null)
    const api = inject('api')
    var reactiveData = reactive({
      citys: null,
      terminalData: crmStore.getters['dictionary/getDictionary']['app_mode'],
      priceNoOptions: [],
      addClientForm: {
        priceNumber: '',
        // provinceCode: '',
        // cityCode: '',
        areaCode: [],
        terminalType: '',
        region: [],
      },
      clientListData: [],
      selectionList: [],
    })
    var formRules = reactive({})
    var formLabelWidth = ref(90)
    var dialogShow = ref(false)
    const handleSelectionChange = (val) => {
      reactiveData.selectionList = val
    }
    const reset = () => {
      // formRef.value.resetFields()
      reactiveData.selectionList.splice(0, reactiveData.selectionList.length)
    }
    const open = (data) => {
      reactiveData.addClientForm.priceNumber = data.priceData.priceNumber
      reactiveData.addClientForm.productPriceId = data.priceData.id
      reactiveData.addClientForm.terminalType = data.priceData.terminalType
      reactiveData.citys = getScreenAreaData(data.priceData.region)

      getTerminalList(reactiveData.addClientForm)
      getPriceList({ productId: data.productId, region: [], terminalType: '' })
      dialogShow.value = true
    }
    const getPriceList = (data) => {
      api.terminal.REGISTERED_TERMINAL_LIST(data).then((res) => {
        if (!res.code && res.data) {
          reactiveData.priceNoOptions = res.data.list
        }
      })
    }
    const close = () => {
      reset()
      dialogShow.value = false
    }
    const getTerminalList = (data) => {
      api.product.PURCHASE_PRICE_ADD_TERMINAL(data).then((res) => {
        if (res.code === 0) {
          console.log(res.data)
          reactiveData.clientListData = res.data?.list || []
        }
      })
    }
    const confirm = () => {
      var select = [...reactiveData.selectionList]
      context.emit('selectSuccess', select)
      dialogShow.value = false
      console.log(reactiveData.selectionList)
    }
    const getAreaCode = (formData) => {
      const a = {
        provinceCode: [],
        cityCode: [],
        areaCode: [],
      }
      for (let i = 0; i < formData.region.length; i++) {
        const area = formData.region[i]
        if (area[0]) {
          a.provinceCode.push(area[0])
        }
        if (area[1]) {
          a.cityCode.push(area[1])
        }
        if (area[2]) {
          a.areaCode.push(area[2])
        }
      }

      return a
    }
    const areaChange = (val) => {
      console.log(val)
      var codes = getAreaCode(reactiveData.addClientForm)
      reactiveData.addClientForm.areaCode = codes.areaCode
      getTerminalList(reactiveData.addClientForm)
    }
    const filterChange = (val) => {
      console.log(val)
      getTerminalList(reactiveData.addClientForm)
    }
    return {
      formRef,
      ...toRefs(reactiveData),
      formRules,
      formLabelWidth,
      dialogShow,
      open,
      close,
      confirm,
      handleSelectionChange,
      areaChange,
      filterChange,
    }
  },
})
</script>
